using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class SelectMissionPanel : MonoBehaviour
{
    public static SelectMissionPanel Instance = null;

    public RectTransform contentRect;

    private List<MissionButton> _missionBtns = new List<MissionButton>();
    private List<GameObject> _missions = new List<GameObject>();

    public void Init()
    {
        if (Instance == null)
        {
            Instance = this;
        }

        ObjectPool.GetInstance().RecycleGo(_missions);

        transform.Find("PlayButton").GetComponent<Button>().onClick.AddListener(OnPlayButtonClick);
        HorizontalLayoutGroup gridLayout = contentRect.GetComponent<HorizontalLayoutGroup>();

        int missionCount = GameManager.Instance._missionDatas.Count;

        for (int i = 0; i < missionCount; i++)
        {
            GameObject go = ObjectPool.GetInstance().SpawnObject(Constants.PathMissionBtn);
            go.transform.SetParent(contentRect);
            MissionButton missionButton = go.GetComponent<MissionButton>();
            _missions.Add(go);
            _missionBtns.Add(missionButton);
            missionButton.Init(i, MissionBtnCallback);
        }

        for (int i = 0; i < _missionBtns.Count; i++)
        {
            _missionBtns[i].SetSelect(GameManager.Instance.MissionIndex,
                GameManager.Instance._missionDatas[i].IsUnlock);
        }

        contentRect.sizeDelta =
            new Vector2(900 * missionCount + gridLayout.spacing * (missionCount - 1) + gridLayout.padding.left + gridLayout.padding.right,
                contentRect.rect.height);
    }

    private void MissionBtnCallback(int missinIndex)
    {
        GameManager.Instance.PlayBtnClickAudio();
        bool isUnlock = GameManager.Instance._missionDatas[missinIndex].IsUnlock;

        if (!isUnlock)
        {
            TipPanel.Instance.ShowPanel("解锁关卡", "请通关前一关卡来解锁此关卡。", action1: () => { }, button1Str: "确认");
            return;
        }

        GameManager.Instance.MissionIndex = missinIndex;

        for (int i = 0; i < _missionBtns.Count; i++)
        {
            _missionBtns[i].SetSelect(missinIndex, GameManager.Instance._missionDatas[i].IsUnlock);
        }
    }


    public void OnPlayButtonClick()
    {
        GameManager.Instance.PlayBtnClickAudio();
        LoadingPanel.Instance.SetPanelActive(true);
        LoadingPanel.Instance.LoadSceneAsync(Constants.MainScene);
        GameManager.Instance.SetPanelState(PanelState.Mission);
        SetPanelActive(false);
    }

    public void SetPanelActive(bool isActive)
    {
        gameObject.SetActive(isActive);
        if (isActive)
        {
            for (int i = 0; i < _missionBtns.Count; i++)
            {
                _missionBtns[i].SetSelect(GameManager.Instance.MissionIndex,
                    GameManager.Instance._missionDatas[i].IsUnlock);
            }

            for (int i = 0; i < _missionBtns.Count; i++)
            {
                _missionBtns[i].ShowBtnAnim(true);
            }
        }
    }
}